Soru & Cevap

Ezan vakti uygulamasında alarm sistemi

17.11.2019 - 11:37

Öncelikle tahmin edebileceğiniz üzere bir gün içerisinde birden fazla alarm kurulan bir sistemim var elimde ve alarmlarımı kurarken kullanıcıdan daha önceden alınan verileride kullanmam gerekiyor. Alarm kurma çalma sırasında sıkıntım yok ancak sorunum alarmları silmekte. Örnek ile anlatmaya çalışacağım; diyelim saar 01:30 ve kullanıcının var olan tüm alarmları kurulu ben saati birden 23:00 yaptığımda aradaki tüm alarmlar çalıyor ve çok kötü bir görüntü oluşuyor uygulama adına. Şimdi uygulamada işleyişi anladığınızı düşünerek soruyorum ben bu alarmları çalmadan nasıl iptal edebilirim. Aşağıya alarmları kuran BroadcastReceiver kodlarını bırakıyorum.

Düzenleme :

Alarmları kurma biçimimde mi br hata var acaba ?

 

 

package com.vaktinde.app.servis;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import androidx.core.content.ContextCompat;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.vaktinde.app.R;
import com.vaktinde.app.activity.MainActivity;
import com.vaktinde.app.constants.Constants;
import com.vaktinde.app.data.SettingDB;
import com.vaktinde.app.entity.Vakit;
import com.vaktinde.app.time.DateRegulative;
import com.vaktinde.app.time.MyDateFormat;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Recep Özen on 21.10.2019.
 */

public class SensorRestarterBroadcastReceiver extends BroadcastReceiver {

    private Context context;
    private Vakit currentVakit;

    @Override
    public void onReceive(final Context context, Intent intent) {
        this.context = context;

        Intent myServiceIntent = new Intent(context, MyService.class);
        if (!servisCalisiyormu()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(myServiceIntent);
                ContextCompat.startForegroundService(context, myServiceIntent);
            } else {
                context.startService(myServiceIntent);
                ContextCompat.startForegroundService(context, myServiceIntent);
            }
            Log.e("Servis", "SensorRestarterBroadcastReceiver Servis başlatıldı");
        }
        //Intent myIntent = new Intent(context, AlarmsDeleteBroadcastReceiver.class);
        //context.sendBroadcast(myIntent);

        List<Vakit> vakitList = onStartCommand();
        if (vakitList != null)
            getcurrentVakit(vakitList);

        Log.e("Servis", "-------------------------------------------");

    }

    public void onClick() {

        List<Integer> getDkList = new ArrayList<>();
        List<Boolean> getVakittenOnceList = new ArrayList<>();
        List<String> vakitler = new ArrayList<>();
        getDkList.add(SettingDB.getInstance(context).getSetting(Constants.IMSAK, 45).getValue_int());
        getDkList.add(SettingDB.getInstance(context).getSetting(Constants.GUNES, 45).getValue_int());
        getDkList.add(SettingDB.getInstance(context).getSetting(Constants.OGLE, 45).getValue_int());
        getDkList.add(SettingDB.getInstance(context).getSetting(Constants.IKINDI, 45).getValue_int());
        getDkList.add(SettingDB.getInstance(context).getSetting(Constants.AKSAM, 45).getValue_int());
        getDkList.add(SettingDB.getInstance(context).getSetting(Constants.YATSI, 45).getValue_int());

        getVakittenOnceList.add(getIntConvertBoolean(SettingDB.getInstance(context).getSetting("vaktinde_once_sabah", 1).getValue_int()));
        getVakittenOnceList.add(getIntConvertBoolean(SettingDB.getInstance(context).getSetting("gunesten_once", 1).getValue_int()));
        getVakittenOnceList.add(getIntConvertBoolean(SettingDB.getInstance(context).getSetting("vaktinde_once_ogle", 1).getValue_int()));
        getVakittenOnceList.add(getIntConvertBoolean(SettingDB.getInstance(context).getSetting("vaktinde_once_ikindi", 1).getValue_int()));
        getVakittenOnceList.add(getIntConvertBoolean(SettingDB.getInstance(context).getSetting("vaktinde_once_aksam", 1).getValue_int()));
        getVakittenOnceList.add(getIntConvertBoolean(SettingDB.getInstance(context).getSetting("vaktinde_once_yatsi", 1).getValue_int()));

        boolean kadin_ozel = getIntConvertBoolean(SettingDB.getInstance(context).getSetting("kadin_ozel", 0).getValue_int());

        if (!kadin_ozel) {

            vakitler.add(currentVakit.getImsak());
            vakitler.add(currentVakit.getGunes());
            vakitler.add(currentVakit.getOgle());
            vakitler.add(currentVakit.getIkindi());
            vakitler.add(currentVakit.getAksam());
            vakitler.add(currentVakit.getYatsi());

            for (int i = 0; i < getDkList.size(); i++) {
                String vakitFark;
                switch (getDkList.get(i)) {
                    case 5:
                        if (getVakittenOnceList.get(i)) {
                            vakitFark = DateRegulative.getInstance().getStringFormat(farkHesap("00:05", vakitler.get(i)));
                            if (getHangiVakit(vakitFark) != null) {
                                setAlarmV(vakitFark, 11 + i);
                            }
                        }
                        break;
                    case 10:
                        if (getVakittenOnceList.get(i)) {
                            vakitFark = DateRegulative.getInstance().getStringFormat(farkHesap("00:10", vakitler.get(i)));
                            if (getHangiVakit(vakitFark) != null) {
                                setAlarmV(vakitFark, 11 + i);
                            }
                        }
                        break;
                    case 15:
                        if (getVakittenOnceList.get(i)) {
                            vakitFark = DateRegulative.getInstance().getStringFormat(farkHesap("00:15", vakitler.get(i)));
                            if (getHangiVakit(vakitFark) != null) {
                                setAlarmV(vakitFark, 11 + i);
                            }
                        }
                        break;
                    case 20:
                        if (getVakittenOnceList.get(i)) {
                            vakitFark = DateRegulative.getInstance().getStringFormat(farkHesap("00:20", vakitler.get(i)));
                            if (getHangiVakit(vakitFark) != null) {
                                setAlarmV(vakitFark, 11 + i);
                            }
                        }
                        break;
                    case 25:
                        if (getVakittenOnceList.get(i)) {
                            vakitFark = DateRegulative.getInstance().getStringFormat(farkHesap("00:25", vakitler.get(i)));
                            if (getHangiVakit(vakitFark) != null) {
                                setAlarmV(vakitFark, 11 + i);
                            }
                        }
                        break;
                    case 30:
                        if (getVakittenOnceList.get(i)) {
                            vakitFark = DateRegulative.getInstance().getStringFormat(farkHesap("00:30", vakitler.get(i)));
                            if (getHangiVakit(vakitFark) != null) {
                                setAlarmV(vakitFark, 11 + i);
                            }
                        }
                        break;
                    case 45:
                        if (getVakittenOnceList.get(i)) {
                            vakitFark = DateRegulative.getInstance().getStringFormat(farkHesap("00:45", vakitler.get(i)));
                            if (getHangiVakit(vakitFark) != null) {
                                setAlarmV(vakitFark, 11 + i);
                            }
                        }
                        break;
                    case 1:
                        if (getVakittenOnceList.get(i)) {
                            vakitFark = DateRegulative.getInstance().getStringFormat(farkHesap("01:00", vakitler.get(i)));
                            if (getHangiVakit(vakitFark) != null) {
                                setAlarmV(vakitFark, 11 + i);
                            }
                        }
                        break;
                }
            }

            List<Boolean> getVaktindeList = new ArrayList<>();
            getVaktindeList.add(getIntConvertBoolean(SettingDB.getInstance(context).getSetting("vaktinde_sabah", 1).getValue_int()));
            getVaktindeList.add(false);
            getVaktindeList.add(getIntConvertBoolean(SettingDB.getInstance(context).getSetting("vaktinde_ogle", 1).getValue_int()));
            getVaktindeList.add(getIntConvertBoolean(SettingDB.getInstance(context).getSetting("vaktinde_ikindi", 1).getValue_int()));
            getVaktindeList.add(getIntConvertBoolean(SettingDB.getInstance(context).getSetting("vaktinde_aksam", 1).getValue_int()));
            getVaktindeList.add(getIntConvertBoolean(SettingDB.getInstance(context).getSetting("vaktinde_yatsi", 1).getValue_int()));

            boolean is_sabah_ezani_imsak = getIntConvertBoolean(SettingDB.getInstance(context).getSetting("is_sabah_ezani_imsak", 0).getValue_int());
            boolean is_vakitler_sessiz = getIntConvertBoolean(SettingDB.getInstance(context).getSetting("is_vakitler_sessiz", 0).getValue_int());
            boolean is_cumada_sessiz = getIntConvertBoolean(SettingDB.getInstance(context).getSetting("is_cumada_sessiz", 0).getValue_int());


            if (getVaktindeList.get(0))
                if (getHangiVakit(currentVakit.getImsak()) != null) {
                    MyDateFormat myDateFormat = farkHesap("01:00", currentVakit.getGunes());
                    String sDate = DateRegulative.getInstance().getStringFormat(myDateFormat);

                    if (!is_sabah_ezani_imsak) {
                        setAlarm(sDate.substring(0, 5), 5);
                        setVakitSessiz(is_vakitler_sessiz, sDate.substring(0, 5), 55, 0);
                    } else {
                        setAlarm(currentVakit.getImsak(), 5);
                        setVakitSessiz(is_vakitler_sessiz, currentVakit.getImsak(), 55, 0);
                    }
                }
            if (getVaktindeList.get(1))
                if (getHangiVakit(currentVakit.getGunes()) != null) {
                    setAlarm(currentVakit.getGunes(), 6);
                }
            if (getVaktindeList.get(2))
                if (getHangiVakit(currentVakit.getOgle()) != null) {
                    setAlarm(currentVakit.getOgle(), 7);

                    Calendar rightNow = Calendar.getInstance();

                    if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY && is_cumada_sessiz) {
                        setCumaSessiz(currentVakit.getOgle());
                    } else {
                        setVakitSessiz(is_vakitler_sessiz, currentVakit.getOgle(), 56, 1);
                    }

                }
            if (getVaktindeList.get(3))
                if (getHangiVakit(currentVakit.getIkindi()) != null) {
                    setAlarm(currentVakit.getIkindi(), 8);
                    setVakitSessiz(is_vakitler_sessiz, currentVakit.getIkindi(), 57, 2);
                }
            if (getVaktindeList.get(4))
                if (getHangiVakit(currentVakit.getAksam()) != null) {
                    setAlarm(currentVakit.getAksam(), 9);
                    setVakitSessiz(is_vakitler_sessiz, currentVakit.getAksam(), 58, 3);
                }
            if (getVaktindeList.get(5))
                if (getHangiVakit(currentVakit.getYatsi()) != null) {
                    setAlarm(currentVakit.getYatsi(), 10);
                    setVakitSessiz(is_vakitler_sessiz, currentVakit.getYatsi(), 59, 4);
                }
        }
    }

    private void setVakitSessiz(boolean is_vakitler_sessiz, String time, int i, int timeZone) {
        if (is_vakitler_sessiz) {
            MyDateFormat myGetDate = farkHesap("00:01", time);
            String date = DateRegulative.getInstance().getStringFormat(myGetDate);
            setAlarmSessizOn(date, i, timeZone);
        }
    }

    private void setCumaSessiz(String time) {


        int cuma_sessiz_once = getIntConverterInt(SettingDB.getInstance(context).getSetting("cuma_sessiz_once", 0).getValue_int());
        String sDate;

        if (cuma_sessiz_once == 0) {
            sDate = "00:" + cuma_sessiz_once + "0";
        } else {
            sDate = "00:" + cuma_sessiz_once;
        }

        if (cuma_sessiz_once == 60) {
            sDate = "01:00";
        }


        MyDateFormat myGetDate = farkHesap(sDate, time);
        String date = DateRegulative.getInstance().getStringFormat(myGetDate);
        setAlarmCumaOn(date);

    }

    private int getIntConverterInt(int sessiz_sabah) {
        List<Integer> list = new ArrayList<>();
        list.clear();
        list.add(0);
        list.add(15);
        list.add(30);
        list.add(45);
        list.add(60);

        switch (sessiz_sabah) {
            case 0:
                return list.get(0);
            case 1:
                return list.get(1);
            case 2:
                return list.get(2);
            case 3:
                return list.get(3);
            case 4:
                return list.get(4);
        }
        return 0;
    }

    private void setAlarm(String hour, int i) {
        @SuppressLint("SimpleDateFormat") final SimpleDateFormat formatter = new SimpleDateFormat("dd:MM:yyyy / HH:mm:ss");

        Calendar setTime = Calendar.getInstance();
        setTime.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hour.substring(0, 2)));
        setTime.set(Calendar.MINUTE, Integer.valueOf(hour.substring(3, 5)));
        setTime.set(Calendar.SECOND, 0);
        setTime.set(Calendar.MILLISECOND, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        Intent broadcastIntent = new Intent(context, VaktindeBroadcastReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, broadcastIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        if (alarmManager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, setTime.getTimeInMillis(), pendingIntent);
            } else {
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, setTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

            }
            Log.e("Vaaktinde", formatter.format(setTime.getTime()) + " " + i);
        }
    }

    private void setAlarmV(String hour, int i) {
        @SuppressLint("SimpleDateFormat") final SimpleDateFormat formatter = new SimpleDateFormat("dd:MM:yyyy / HH:mm:ss");

        Calendar setTime = Calendar.getInstance();
        setTime.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hour.substring(0, 2)));
        setTime.set(Calendar.MINUTE, Integer.valueOf(hour.substring(3, 5)));
        setTime.set(Calendar.SECOND, 0);
        setTime.set(Calendar.MILLISECOND, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        Intent broadcastIntent = new Intent(context, VaktindenOnceBroadcastReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, broadcastIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        if (alarmManager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, setTime.getTimeInMillis(), pendingIntent);
            } else {
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, setTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

            }
            Log.e("VaktindenOnce", formatter.format(setTime.getTime()) + " " + i);
        }
    }

    private void setAlarmSessizOn(String hour, int i, int timeZone) {
        @SuppressLint("SimpleDateFormat") final SimpleDateFormat formatter = new SimpleDateFormat("dd:MM:yyyy / HH:mm:ss");

        Calendar setTime = Calendar.getInstance();
        setTime.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hour.substring(0, 2)));
        setTime.set(Calendar.MINUTE, Integer.valueOf(hour.substring(3, 5)));
        setTime.set(Calendar.SECOND, 0);
        setTime.set(Calendar.MILLISECOND, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        Intent broadcastIntent = new Intent(context, SessizOnBroadcastReceiver.class);
        broadcastIntent.putExtra("time_zone", timeZone);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, broadcastIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        if (alarmManager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, setTime.getTimeInMillis(), pendingIntent);
            } else {
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, setTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
            }
            Log.e("SessizOn", formatter.format(setTime.getTime()) + " " + i);
        }
    }

    private void setAlarmCumaOn(String hour) {
        @SuppressLint("SimpleDateFormat") final SimpleDateFormat formatter = new SimpleDateFormat("dd:MM:yyyy / HH:mm:ss");

        Calendar setTime = Calendar.getInstance();
        setTime.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hour.substring(0, 2)));
        setTime.set(Calendar.MINUTE, Integer.valueOf(hour.substring(3, 5)));
        setTime.set(Calendar.SECOND, 0);
        setTime.set(Calendar.MILLISECOND, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        Intent broadcastIntent = new Intent(context, CumaOnBroadcastReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 60, broadcastIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        if (alarmManager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, setTime.getTimeInMillis(), pendingIntent);
            } else {
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, setTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
            }
            Log.e("CumaOn", formatter.format(setTime.getTime()) + " " + 60);
        }

    }

    private void getcurrentVakit(List<Vakit> vakitList) {

        @SuppressLint("SimpleDateFormat") SimpleDateFormat formatterOnlyDate = new SimpleDateFormat("dd.MM.yyyy");

        Date date = new Date(System.currentTimeMillis());
        String sDate = formatterOnlyDate.format(date);

        for (int i = 0; i < vakitList.size(); i++) {
            if (sDate.equals(vakitList.get(i).getMiladiTarihKisa())) {
                currentVakit = vakitList.get(i);
                onClick();
                break;
            }
        }
    }

    private String getSTR(int str) {
        return context.getResources().getString(str);
    }

    public List<Vakit> onStartCommand() {

        String response = SettingDB.getInstance(context).getSetting("vakitler", null).getValue_str();
        Constants.selectedTownCod = SettingDB.getInstance(context).getSetting("selectedTownCod", null).getValue_str();

        List<Vakit> vakitList = new ArrayList<>();
        if (response == null) {
            if (Constants.selectedTownCod != null) {
                getVakitler(getSTR(R.string.api_town) + Constants.selectedTownCod);
                return Constants.vakitList;
            }

        } else {
            try {
                byte[] u = response.getBytes(
                        "ISO-8859-1");

                response = new String(u, "UTF-8");

                Gson gson = new Gson();
                Type listType = new TypeToken<ArrayList<Vakit>>() {
                }.getType();
                vakitList = gson.fromJson(response, listType);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            if (vakitList != null && vakitList.size() > 1) {
                Constants.vakitList = vakitList;
                return vakitList;
            }
        }
        return null;
    }

    private void getVakitler(String postUrl) {

        final RequestQueue queue = Volley.newRequestQueue(context);
        StringRequest postRequest = new StringRequest(Request.Method.GET, postUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        if (!response.equals("")) {
                            List<Vakit> vakitList = new ArrayList<>();

                            try {
                                byte[] u = response.getBytes(
                                        "ISO-8859-1");
                                response = new String(u, "UTF-8");

                                Gson gson = new Gson();
                                Type listType = new TypeToken<ArrayList<Vakit>>() {
                                }.getType();
                                vakitList = gson.fromJson(response, listType);

                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }

                            if (vakitList != null && vakitList.size() > 1) {
                                Constants.vakitList = vakitList;
                                SettingDB.getInstance(context).duzenle("vakitler", response);
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                }
        );
        queue.add(postRequest);
    }

    private String getHangiVakit(String vakit) {

        MyDateFormat myDateFormat = farkHesap(vakit);

        if (myDateFormat.getsDay() == 0 && myDateFormat.getsMonth() == 0 && myDateFormat.getsYear() == 0) {
            if (myDateFormat.getsHour() != 0 || myDateFormat.getsMinute() != 0 || myDateFormat.getsSecond() != 0) {

                //Log.e("Geçerli Vakit: ", vakit);

                return vakit;
            }
        }
        return null;
    }

    private MyDateFormat farkHesap(String vakit) {
        String h = "0";
        String m = "0";
        if (vakit != null && !vakit.equals("")) {
            h = vakit.substring(0, 2);
            m = vakit.substring(3, 5);
        }
        Calendar currentDiffirentTime = Calendar.getInstance();
        currentDiffirentTime.set(Calendar.YEAR, 0);
        currentDiffirentTime.set(Calendar.MONTH, 0);
        currentDiffirentTime.set(Calendar.DAY_OF_MONTH, 0);

        Calendar setTime = Calendar.getInstance();
        setTime.set(Calendar.YEAR, 0);
        setTime.set(Calendar.MONTH, 0);
        setTime.set(Calendar.DAY_OF_MONTH, 0);
        setTime.set(Calendar.HOUR_OF_DAY, Integer.valueOf(h));
        setTime.set(Calendar.MINUTE, Integer.valueOf(m));
        setTime.set(Calendar.SECOND, 0);

        return DateRegulative.getInstance().getDifference(setTime.getTimeInMillis(), currentDiffirentTime.getTimeInMillis());
    }

    private MyDateFormat farkHesap(String simdikiVakit, String birSonrakiVakit) {
        String h1 = simdikiVakit.substring(0, 2);
        String m1 = simdikiVakit.substring(3, 5);

        String h2 = birSonrakiVakit.substring(0, 2);
        String m2 = birSonrakiVakit.substring(3, 5);

        Calendar oldTime = Calendar.getInstance();
        oldTime.set(Calendar.YEAR, 0);
        oldTime.set(Calendar.MONTH, 0);
        oldTime.set(Calendar.DAY_OF_MONTH, 0);
        oldTime.set(Calendar.HOUR_OF_DAY, Integer.valueOf(h1));
        oldTime.set(Calendar.MINUTE, Integer.valueOf(m1));
        oldTime.set(Calendar.SECOND, 0);

        Calendar currentTime = Calendar.getInstance();
        currentTime.set(Calendar.YEAR, 0);
        currentTime.set(Calendar.MONTH, 0);
        currentTime.set(Calendar.DAY_OF_MONTH, 0);
        currentTime.set(Calendar.HOUR_OF_DAY, Integer.valueOf(h2));
        currentTime.set(Calendar.MINUTE, Integer.valueOf(m2));
        currentTime.set(Calendar.SECOND, 0);

        return DateRegulative.getInstance().getDifference(currentTime.getTimeInMillis(), oldTime.getTimeInMillis());
    }

    private boolean getIntConvertBoolean(int vaktinde_sabah) {
        return vaktinde_sabah == 1;
    }

    public boolean servisCalisiyormu() {

        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (MyService.class.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
}

 

Ayrıca alarmları silmek için önceden yazdığım ama işlevi olmayan kodlarıda buraya koyuyorum ;

 

package com.vaktinde.app.servis;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import androidx.core.content.ContextCompat;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.vaktinde.app.R;
import com.vaktinde.app.constants.Constants;
import com.vaktinde.app.data.SettingDB;
import com.vaktinde.app.entity.Vakit;
import com.vaktinde.app.time.DateRegulative;
import com.vaktinde.app.time.MyDateFormat;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by recep on 21.03.2017.
 */

public class AlarmsDeleteBroadcastReceiver extends BroadcastReceiver {

    private Context context;

    @Override
    public void onReceive(final Context context, Intent intent) {
        this.context = context;
        onClick();

    }

    public void onClick() {

        // Alarmların sıfırlandığı bölüm
        List<Integer> requestCodList_00 = new ArrayList<>();
        //Vakitlerde sessiz istek kodları
        requestCodList_00.add(55);
        requestCodList_00.add(56);
        requestCodList_00.add(57);
        requestCodList_00.add(58);
        requestCodList_00.add(59);
        for (int i : requestCodList_00) {
            alarmKillerSessiz(i);
        }

        alarmKillerCuma(60); // cuma sessiz istek kodu

        List<Integer> requestCodList_01 = new ArrayList<>();
        //Vakitlerden önce kullanılan istek kodları
        requestCodList_01.add(11);
        requestCodList_01.add(12);
        requestCodList_01.add(13);
        requestCodList_01.add(14);
        requestCodList_01.add(15);
        requestCodList_01.add(16);
        requestCodList_01.add(99); // Vaktinde Önce Erteleme İstek Kodu
        for (int i : requestCodList_01) {
            alarmKillerVaktindenOnce(i);
        }

        List<Integer> requestCodList_02 = new ArrayList<>();
        //Vakitlerde kullanılan istek kodları
        requestCodList_02.add(5);
        requestCodList_02.add(6);
        requestCodList_02.add(7);
        requestCodList_02.add(8);
        requestCodList_02.add(9);
        requestCodList_02.add(10);
        for (int i : requestCodList_02) {
            alarmKillerVaktinde(i);
        }

        /*Intent broadcastIntent = new Intent(context, SensorRestarterBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 120, broadcastIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null) {
            alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    AlarmManager.INTERVAL_HALF_HOUR,
                    AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
        }*/

    }

    private void alarmKillerVaktindenOnce(int requestCod) {

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent broadcastIntent = new Intent(context, VaktindenOnceBroadcastReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCod, broadcastIntent, 0);

        if (alarmManager != null && pendingIntent != null) {
            alarmManager.cancel(pendingIntent);
            Log.e("alarmKiller", requestCod + " istek kodlu alarm iptal edildi.");
        }
    }

    private void alarmKillerVaktinde(int requestCod) {

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent broadcastIntent = new Intent(context, VaktindeBroadcastReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCod, broadcastIntent, 0);

        if (alarmManager != null && pendingIntent != null) {
            alarmManager.cancel(pendingIntent);
            Log.e("alarmKiller", requestCod + " istek kodlu alarm iptal edildi.");
        }
    }

    private void alarmKillerSessiz(int requestCod) {

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent broadcastIntent = new Intent(context, SessizOnBroadcastReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCod, broadcastIntent, 0);

        if (alarmManager != null && pendingIntent != null) {
            alarmManager.cancel(pendingIntent);
            Log.e("alarmKiller", requestCod + " istek kodlu alarm iptal edildi.");
        }
    }

    private void alarmKillerCuma(int requestCod) {

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent broadcastIntent = new Intent(context, CumaOnBroadcastReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCod, broadcastIntent, 0);

        if (alarmManager != null && pendingIntent != null) {
            alarmManager.cancel(pendingIntent);
            Log.e("alarmKiller", requestCod + " istek kodlu alarm iptal edildi.");
        }
    }

    private String getSTR(int str) {
        return context.getResources().getString(str);
    }

}

 

95 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-259959-1585770739.jpg
ertan.kocakoc
01.12.2019 - 01:13

Merhaba, https://www.youtube.com/watch?v=QZKefdd-22c bu video da anlatım yapılmış. İnceleyebilirsiniz.

Recep Özen
01.12.2019 - 01:15
Teşekkür ederim bakıcam.